home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14224 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.2 KB

  1. Path: EU.net!sun4nl!ittpub!ittpub!nntp
  2. Newsgroups: comp.lang.c++
  3. Subject: Re: Derivation and calling virtual functions
  4. Message-ID: <1996Mar29.152758.1812@ittpub>
  5. From: wil@ittpub.nl (Wil Evers)
  6. Date: 29 Mar 96 15:27:58 WET
  7. References: <graphix.828032689@spiff.cc.iastate.edu>
  8. Distribution: world
  9. Nntp-Posting-Host: lintilla
  10.  
  11. In article <graphix.828032689@spiff.cc.iastate.edu> graphix@iastate.edu  
  12. (Kent A Vander Velden) writes:
  13.  
  14. >   Say we have the following:
  15. > class Base {
  16. > public:
  17. >   virtual void func() { // some stuff }
  18. >   virtual void write() { func(); };
  19. > };
  20. > class Derived : public Base {
  21. > public:
  22. >   void func() { // some stuff }
  23. >   write(); { Base::func(); }
  24. > };
  25. > Derived inst;
  26. >   Now, when I call inst.write() it in turn calls Bass::write() which in
  27. > turn calls Base::func().  Is there a way to instead have it call
  28. > Derived::func() if the instance is of type Derived?  I hoped the 
  29. > virtual keyword would help in this case.
  30.  
  31. You write Derived::write() will call Base::write(), but your code says  
  32. Derived::write() calls Base::func(). If you change your code to have  
  33. Derived::write() call Base::write(), then Base::write() will call the  
  34. overriding Derived::func().
  35.  
  36. Hope this helps,
  37.  
  38. - Wil
  39.